
package Commons.Logging;

import Commons.TaskDescriptor;
import Commons.WebServiceClient.VectorClock;
import Commons.WorkerDescriptor;
import Commons.Workflow;
import Commons.WriterXML;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;



/**
 *
 * @author Team5
 */
public class LoggerContenutoCode implements Runnable{
    private File filelog;
    private Thread scritturaLocale;
    private ArrayList<Object> codaArray;
    private WriterXML wXml;
    private Object obj;
       /**
     * Costruttore di default.
     */
   public LoggerContenutoCode()  throws XMLStreamException, IOException{
       //vc = new VectorClock();
       wXml = new WriterXML("/home/carlo/Scrivania/test2.xml");
       scritturaLocale = new Thread(this);
    }
    /**Costruttore con parametri
     *
     * @param nomeFileXML
     */
   public LoggerContenutoCode(String pathFileXML) throws XMLStreamException{
       //vc = new VectorClock();
       wXml=new WriterXML(pathFileXML);
       scritturaLocale = new Thread(this);
    }

    public void logging(Object obj) {//esegue un thread per la scrittura del log sul file e un altro thread per la comunicazione del log al monitor globale

        try {
            //esegue un thread per la scrittura del log sul file e un altro thread per la comunicazione del log al monitor globale
            codaArray = (ArrayList) obj;
            wXml.startObjXML();
            Iterator iter = codaArray.iterator();
            while (iter.hasNext()) {
                try {
                    scritturaLocale.join();
                    this.obj = iter.next();
                    scritturaLocale = new Thread(this);
                    scritturaLocale.start();
                } catch (InterruptedException ex) {
                    Logger.getLogger(LoggerContenutoCode.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            this.scritturaLocale.join();
            wXml.closeObjXML();
        } catch (InterruptedException ex) {
            Logger.getLogger(LoggerContenutoCode.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
   
   
  

    @Override
    public void run() {
        try {
            wXml.writeObjXML(obj);
        } catch (IOException ex) {
            Logger.getLogger(LoggerContenutoCode.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    

}
